/**
 * This class makes file reading and writing easier.
 */
package meltwater.test;

import java.util.*;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.ByteBuffer;
import java.io.*;
 
/**
 * @author joconnor
 *
 */
public class FileUtil {
 
    // Turns a file into a string
    static String readFile(String path ) 
    {
        try { 
            byte[] encoded = Files.readAllBytes(Paths.get(path));
            return Charset.defaultCharset().decode(ByteBuffer.wrap(encoded)).toString();
        }
        catch( IOException e ) {
            System.out.println( "Couldnt open file: " + path );
            System.exit(0);
        }
        return null;
    }

    // Prints to a file
    static void writeFile( String path, String toWrite ) {
        Writer writer = null;

        try {
            writer = new BufferedWriter(new OutputStreamWriter(
                  new FileOutputStream(path), "utf-8"));
            writer.write(toWrite);
        } catch (IOException ex) {
          // report
        } finally {
            try {writer.close();} catch (Exception ex) {}
        }
    }

    // Makes a new directory
    static void mkdir( String path ) {
        boolean success = (new File(path).mkdirs());
        if (!success) { 
        //    System.out.println( "Couldnt open file: " + path );
        //    System.exit(0);
        }
    }
}
